package g11.dao;

import g11.model.User;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class UserDAOImpl implements UserDAO {

	private static final Logger log = Logger.getLogger(PatientDAOImpl.class);

	private Session session;

	public Session getSession() {
		return this.session;
	}

	public void cleanup() {
		if (session.isOpen())
			session.close();
		// HibernateUtil.shutdown();
	}

	public UserDAOImpl() {

	}

	public Integer createUser(User user) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		Integer userId;
		try {
			tran = session.beginTransaction();
			userId = (Integer) session.save(user);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return userId;
	}

	public void updateUser(User user) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.update(user);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
	}

	public void deleteUser(User user) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.delete(user);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
	}

	public User getUserbyUsername(String username, String password, byte type)
			throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		User user = null;
		try {
			String hql = "from User u where u.username=? and u.password=? and u.type=?";
			Query q = session.createQuery(hql);
			q.setString(0, username);
			q.setString(1, password);
			q.setByte(2, type);
			System.out.print(q.getQueryString());
			if (q.list().size() > 0) {
				user = (User) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return user;
	}

	public User getUserByUsername(String username, String password)
			throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		User user = null;
		try {
			//
			String hql = "from User u where u.username=? and u.password=?";
			Query q = session.createQuery(hql);
			q.setString(0, username);
			q.setString(1, password);
			System.out.print(q.getQueryString());
			if (q.list().size() > 0) {
				user = (User) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return user;
	}

}
